const { USER, ADMIN, GUESTPIECE, USERCONTENT, USERCONTENTINFO, INDEXIMGLIST, INDEXZUOPIN, INDEXWRAPPER, INDEXFOOTER, HOMEBRAND } = require('./database')
const { guestpieceCount, UsercontentCount } = require('../config')
const query = require('./fun')

/**
 * 所有请求方法
 */
/**
 *  登录
 */
function isLogin(data) {
    return new Promise((resolve, reject) => {
        if (data.radio == 'admin') {
            query(`select  * from ${ADMIN} where username=? and password=?`, data)
                .then((res) => {
                    resolve(res)
                })
                .catch((err) => {
                    reject(reject)
                })
        } else {
            query(`select  * from ${USER} where username=? and password=?`, data)
                .then((res) => {
                    resolve(res)
                })
                .catch((err) => {
                    reject(reject)
                })
        }
    })
}
/**
 * 判断是否存在注册用户
 */
function isUser(data) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${USER} where username=? `, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 注册用户
 */
function registerUser(data) {
    return new Promise((resolve, reject) => {
        query(`INSERT INTO ${USER} VALUES (null,?,?)`, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取真实客片信息的长度
 */
function getGuestpieceInfoCount() {
    return new Promise((resolve, reject) => {
        query(`select count(id) as counts from ${GUESTPIECE}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取真实客片信息
 */
function getGuestpieceInfo(page) {
    return new Promise((resolve, reject) => {
        query(`select * from ${GUESTPIECE} limit ?,${guestpieceCount}`, { page })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取用户评论的长度
 */
function getUsercontentCount() {
    return new Promise((resolve, reject) => {
        query(`select count(id) as counts from ${USERCONTENT}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取用户评论信息
 */
function getUsercontentInfo(page) {
    return new Promise((resolve, reject) => {
        query(`select * from ${USERCONTENT} limit ?,${UsercontentCount}`, { page })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取指定用户评论Id的照片集合
 */
function getUsercontentInfoListById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${USERCONTENTINFO} where cid=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取首页的轮播数据
 */
function getIndexlbimgList() {
    return new Promise((resolve, reject) => {
        query(`select * from ${INDEXIMGLIST}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取首页的作品数据
 */
function getIndexZuopin() {
    return new Promise((resolve, reject) => {
        query(`select * from ${INDEXZUOPIN}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取首页的底部轮播数据
 */
function getIndexWrapper() {
    return new Promise((resolve, reject) => {
        query(`select * from ${INDEXWRAPPER}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取首页的底部数据
 */
function getIndexFooter() {
    return new Promise((resolve, reject) => {
        query(`select * from ${INDEXFOOTER}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取介绍信息
 */
function getHomeBrand() {
    return new Promise((resolve, reject) => {
        query(`select * from ${HOMEBRAND}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 保存用户评论信息
 */
function saveUserContent(data) {
    return new Promise((resolve, reject) => {
        query(`INSERT INTO ${USERCONTENT} VALUES (null,?,?,?,?)`, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
exports.isLogin = isLogin;
exports.isUser = isUser;
exports.registerUser = registerUser;
exports.getGuestpieceInfo = getGuestpieceInfo;
exports.getGuestpieceInfoCount = getGuestpieceInfoCount;
exports.getUsercontentCount = getUsercontentCount;
exports.getUsercontentInfo = getUsercontentInfo;
exports.getUsercontentInfoListById = getUsercontentInfoListById;
exports.getIndexlbimgList = getIndexlbimgList;
exports.getIndexZuopin = getIndexZuopin;
exports.getIndexWrapper = getIndexWrapper;
exports.getIndexFooter = getIndexFooter;
exports.getHomeBrand = getHomeBrand;
exports.saveUserContent = saveUserContent;